package com.jielin.handling.controller.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jielin.handling.bean.AppResult;
import com.jielin.handling.bean.user.User;
import com.jielin.handling.bean.user.UserSearch;
import com.jielin.handling.bean.user.UserSession;
import com.jielin.handling.exception.MyException;
import com.jielin.handling.page.DataGrid;
import com.jielin.handling.redis.RedisTemplateX;
import com.jielin.handling.service.user.UserService;
import com.jielin.handling.service.user.UserSessionService;

import lombok.extern.slf4j.Slf4j;

/**
 * 类名称：UserController
 * 创建人：zhanglongdeng@163.com
 * 创建时间：2019年3月5日 下午3:05:41   
 * 类描述：
 */
@RestController
@RequestMapping("/user")
@Slf4j
@CrossOrigin
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private UserSessionService userSessionService;
	@Autowired
	private RedisTemplateX redisTemplateX;

	@RequestMapping(value="/init")
	public String init(){
		log.info("web服务测试成功......");
		userService.test();
		return "success";
	}
	
	@RequestMapping(value="/initT")
	public String initT() throws Exception{
		log.info("web服务事务测试......");
		try {
			userService.testTransaction();
		} catch (Exception e) {
			return "error";
		}
		return "success";
	}
	
	@RequestMapping(value="/testE")
	public AppResult testE(int value) throws Exception{
		if(value == 0) {
			throw new MyException(-1, "参数缺失");
		}
		if(value == 1) {
			throw new Exception("hhhhhhhhhhhhhhh");
		}
		return AppResult.builder().setCode(200).setMsg("异常测试");
	}
	
	@RequestMapping(value="/insert")
	public AppResult insert(User user){
		AppResult result = new AppResult();
		JSONObject data = new JSONObject();
		log.info("user:{}",JSONObject.toJSONString(user));
		long value = userService.insertUser(user);
		if(value>0) {
			data.put("value", 1);
			result.setCode(200);
			result.setMsg("insert success");
			result.setData(data);
		}else {
			data.put("value", 0);
			result.setCode(-1);
			result.setMsg("insert error");
			result.setData(data);
		}
		return result;
	}
	
	/**
	 * session测试
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/session")
	public AppResult session(HttpServletRequest request){
		AppResult result = new AppResult();
		JSONObject data = new JSONObject();
		UserSession session = userSessionService.getUserSession(request);
		log.info("session===>{}",session);
		data.put("session", session);
		result.setCode(200);
		result.setMsg("success");
		result.setData(data);
		return result;
	}
	
	@RequestMapping(value="/list")
	public AppResult list(){
		AppResult result = new AppResult();
		JSONObject data = new JSONObject();
		log.info("user list...");
		log.info("redisTemplateX===>{}",redisTemplateX);
		redisTemplateX.set("longdeng", "123456");
		log.info("redisTemplateX.get===>{}",redisTemplateX.get("longdeng"));
		List<User> list = userService.getUserList();
		data.put("list", list);
		result.setCode(200);
		result.setMsg("query success");
		result.setData(data);
		return result;
	}
	
	@RequestMapping(value="/query")
	public AppResult query(int pageNum,int pageSize){
		JSONObject queryCondition = null;
		AppResult result = new AppResult();
		JSONObject data = new JSONObject();
		log.info("user query...");
		//查询条件+分页
		UserSearch search = new UserSearch();
		search.setQueryCondition(queryCondition);
		search.setPage(pageNum, pageSize);
		List<User> orderList = userService.getUserListByPage(search);
		//用PageInfo对结果进行包装
		PageInfo<User> pageInfo = new PageInfo<>(orderList);
		DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
		data.put("datagrid", dataGrid);
		result.setCode(200);
		result.setMsg("query success");
		result.setData(data);
		return result;
	}
	
	@RequestMapping(value="/queryCondition")
	public AppResult queryCondition(@RequestBody JSONObject queryCondition){
		AppResult result = new AppResult();
		JSONObject data = new JSONObject();
		int pageNum = queryCondition.getIntValue("pageNum");
		int pageSize = queryCondition.getIntValue("pageSize");
		log.info("user query...");
		//查询条件+分页
		UserSearch search = new UserSearch();
		search.setQueryCondition(queryCondition);
		search.setPage(pageNum, pageSize);
		List<User> orderList = userService.getUserListByPage(search);
		//用PageInfo对结果进行包装
		PageInfo<User> pageInfo = new PageInfo<>(orderList);
		DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
		data.put("datagrid", dataGrid);
		result.setCode(200);
		result.setMsg("query success");
		result.setData(data);
		return result;
	}
	
}
